/* 
    Copyright (c) 2011 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the 
    license agreement between you and Microsoft.
  
    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604 
  
*/
using System;
using System.Linq;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TagCloudNewsLibrary;
using TagCloudNewsLibrary.Model;
using System.Windows.Media;

namespace TagCloudNews
{
    public partial class StoriesPage : PhoneApplicationPage
    {
        // Constructor
        public StoriesPage()
        {
            InitializeComponent();

            TagDataService.Current.LoadCompletedEvent = new LoadCompletedEventHandler(LoadCompleted);
            TagDataService.Current.LoadFailedEvent = new LoadFailedEventHandler(LoadFailed);
        }

        // This method determines whether the user has navigated to the application after the application was tombstoned.
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            TagDataService.Current.ForceLoad(false);
        }

        //if called from the start screen
        public void LoadCompleted()
        {
            string tag;

            if (NavigationContext.QueryString.TryGetValue("tag", out tag))
            {
                DataContext = TagDataService.Current.GetStoriesForTag(TagDataService.Current.UnescapeTag(tag));
            }
        }

        //if called from the start screen
        public void LoadFailed()
        {
        }

        // The SelectionChanged handler for the feed items 
        private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            if (listBox != null && listBox.SelectedItem != null)
            {
                // Get the SyndicationItem that was tapped.
                Story sItem = (Story)listBox.SelectedItem;

                string title = sItem.Title;
                PhoneApplicationService.Current.State["title"] = title as object; 

                NavigationService.Navigate(new Uri("/ViewerPage.xaml", UriKind.Relative));
            }
        }

        private void LikeButton_Click(object sender, EventArgs e)
        {
            TagDataService.Current.ToggleLikeTag(PageTitle.Text);
            bool b=TagDataService.Current.IsTagLiked(PageTitle.Text);
            LikeStatus.Text = (b ? "Liked" : "Unliked");
            LikeStatus.Foreground = (b ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.White));
        }

        private void ReadAllButton_Click(object sender, EventArgs e)
        {
            TagDataService.Current.ReadAllStoriesForTag(PageTitle.Text);
            var model = TagDataService.Current.GetStoriesForTag(PageTitle.Text);
            feedListBox.ItemsSource = model.Stories;
        }

        private void PinButton_Click(object sender, EventArgs e)
        {
            IconicTileData oIcontile = new IconicTileData();
            oIcontile.Title = PageTitle.Text;
            oIcontile.Count = feedListBox.Items.Count;

            oIcontile.IconImage = new Uri(@"202x202.png", UriKind.Relative);
            oIcontile.SmallIconImage = new Uri(@"110x110.png", UriKind.Relative);
 
            // find the tile object for the application tile that using "Iconic" contains string in it.
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("Iconic".ToString()));
 
            if (TileToFind != null && TileToFind.NavigationUri.ToString().Contains("Iconic"))
            {
                TileToFind.Delete();
            }
            ShellTile.Create(new Uri("/StoriesPage.xaml?tag=" + TagDataService.Current.EscapeTag( PageTitle.Text), UriKind.Relative), oIcontile, true);
        }       
    }
}
